package com.yfjin.webrtc.ws;

import android.content.Context;
import android.util.Log;

import com.yfjin.webrtc.util.IpUtil;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

//音视频通话客户端
public class SocketLive {

    private static final String TAG = "123123";


    private final int mPort = 7033;
    private final String[] mUrls = {"ws://192.168.1.46:", "ws://192.168.1.45:", "ws://192.168.1.53:"};


    public SocketLive(IPeerConnection peerConnection) {
        mWsServer = new WsServer(peerConnection, mPort);
        mWsServer.start();
    }

    private Set<WsClient> mSocketClientList = new HashSet<>();
    private WsServer mWsServer;
    private Timer mTimer;

    public void start(final Context context) {

        for (String value : mUrls) {
            if (value.contains(IpUtil.getLocalIpAddress(context))) {
                continue;
            }
            URI url = null;
            try {
                url = new URI(value + mPort);
                WsClient wsClient = new WsClient(value, url);
                wsClient.setCallback(new WsClient.IWsClientCallback() {
                    @Override
                    public void onConenct(WsClient ws) {
                        mSocketClientList.add(ws);
                    }

                    @Override
                    public void onDisConnect(WsClient ws) {
                        if (mSocketClientList.contains(ws)) {
                            mSocketClientList.remove(ws);
                        }
                    }
                });
                wsClient.connect();

            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }


        //模拟进入房间时，其他人主动需要 与新加入的人产生连接
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                for (String value : mUrls) {
                    //排除本地连接
                    if (value.contains(IpUtil.getLocalIpAddress(context))) {
                        continue;
                    }
                    boolean isSame = false;
                    //排出重复连接
                    for (WsClient wsClient : mSocketClientList) {
                        if (value.contains(wsClient.getUrl())) {
                            isSame = true;
                            break;
                        }
                    }
                    if (isSame) {
                        continue;
                    }
                    URI url = null;
                    try {
                        url = new URI(value + mPort);
                        WsClient myWebSocketClient = new WsClient(value, url);
                        myWebSocketClient.connect();
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    }
                }

                Log.i(TAG, "run: ------------延迟--------");
            }
        }, 3 * 1000, 5 * 1000);
    }


    public void sendData(byte[] bytes) {
        for (WsClient client : mSocketClientList) {
            if (client.isOpen()) {
                client.send(bytes);
            }
        }

    }

}
